package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import client.ClientConsole;

import logic.Message;
import logic.PeriodicReport;

/** 
 * produces and shows the periodic report
 * shows statistic information for readers
 */
public class PeriodicReportWindow extends JPanel implements ActionListener{
	/**  GUI Panels used for Design*/
	private JPanel mainPanel,reportPanel,backbottom,topPanel,prevNextButtonsPanel;
	/**error label show the feedback from server*/
	private JLabel frozenLabel,lockedLabel,activeLabel,borrowedBooksLabel,delayedInReturnLabel,frozen,
	locked,active,borrowedBooks,delayedInReturn,error,dateLabel;
	/** next and previous button are being used scroll through all periodic reports*/
	private JButton back,nextButton,prevButton;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	/**Holds the number report in history*/
	private int numOfReports;
	/**Holds the number report to be shown on screen*/
	private int currentReportNumber;
	/** array that holds all periodic reports*/
	PeriodicReport[] pr;
	
	/**Default constructor-Generates and create the periodic reports*/
	public PeriodicReportWindow()
	{
		super();
		setSize(300, 300);
		numOfReports=0;
		currentReportNumber=1;
		pr=null;
		setLayout(new BorderLayout());
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		topPanel=new JPanel();
		topPanel.setLayout(new BorderLayout());
		dateLabel=new JLabel();
		topPanel.add(dateLabel,BorderLayout.WEST);
		mainPanel.add(topPanel,BorderLayout.NORTH);
		reportPanel=new JPanel();
		reportPanel.setLayout(new GridBagLayout());
		gbc.insets= new Insets(7,7,7,7);
		frozenLabel=new JLabel("Frozen Readers: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(frozenLabel,gbc);
		frozen=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(frozen,gbc);
		lockedLabel=new JLabel("Locked Readers: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(lockedLabel,gbc);
		locked=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(locked,gbc);
		activeLabel=new JLabel("Active Readers: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=2;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(activeLabel,gbc);
		active=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(active,gbc);
		borrowedBooksLabel=new JLabel("Number of borrowed books: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=3;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(borrowedBooksLabel,gbc);
		borrowedBooks=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=3;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(borrowedBooks,gbc);
		delayedInReturnLabel=new JLabel("Number of book delayed in return: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=4;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(delayedInReturnLabel,gbc);
		delayedInReturn=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=4;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(delayedInReturn,gbc);
		prevNextButtonsPanel=new JPanel();
		prevButton= new JButton("Previous Report");
		nextButton=new JButton("Next Report");
		prevNextButtonsPanel.add(prevButton);
		prevNextButtonsPanel.add(nextButton);
		prevButton.addActionListener(this);
		nextButton.addActionListener(this);
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=5;
		gbc.gridwidth=2;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(prevNextButtonsPanel,gbc);
		
		
		mainPanel.add(reportPanel,BorderLayout.CENTER);
		back=new JButton("back");
		backbottom=new JPanel();
		backbottom.add(back);
		error=new JLabel();
		error.setVisible(false);
		backbottom.add(error);
		back.addActionListener(this);
		mainPanel.add(backbottom,BorderLayout.SOUTH);
		add(mainPanel,BorderLayout.CENTER);
		produceReport();
		
		
	}
	/**
	 * sends a request to generate new Report and show its properties
	 */
	private void produceReport() {
		Message msg=new Message ("Get Periodic Report",null);
		ClientConsole.HandleMessageFromGui(msg);
		msg=(Message)ClientConsole.client.getMessage();
		if (msg.getCommand().equals("New report added and returned all periodic report"))
		{
			if (((PeriodicReport[]) msg.getObjects()).length>0)
			{
				String pattern="dd/MM/yyyy";
				SimpleDateFormat newDate=new SimpleDateFormat(pattern);
				
				pr=(PeriodicReport[]) msg.getObjects();
				numOfReports=pr.length;
				frozen.setText(String.valueOf(pr[currentReportNumber-1].getFrozenReaders()));
				locked.setText(String.valueOf(pr[currentReportNumber-1].getLockedReaders()));
				active.setText(String.valueOf(pr[currentReportNumber-1].getActivatedReaders()));
				borrowedBooks.setText(String.valueOf(pr[currentReportNumber-1].getNumberOfBorrowedBooks()));
				delayedInReturn.setText(String.valueOf(pr[currentReportNumber-1].getNumberOfDelayedInReturns()));
				dateLabel.setText(newDate.format(pr[currentReportNumber-1].getReportDate()));
				prevButton.setVisible(false);
				if (numOfReports==1)
					nextButton.setVisible(false);
				
			}
			
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==back)
		{
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new ProduceReportWindow());
		}
		//view the newer report in DB
		else if (ae.getSource()==prevButton)
		{
			String pattern="dd/MM/yyyy";
			SimpleDateFormat newDate=new SimpleDateFormat(pattern);
			currentReportNumber--;
			if (currentReportNumber<=1)
				prevButton.setVisible(false);
			frozen.setText(String.valueOf(pr[currentReportNumber-1].getFrozenReaders()));
			locked.setText(String.valueOf(pr[currentReportNumber-1].getLockedReaders()));
			active.setText(String.valueOf(pr[currentReportNumber-1].getActivatedReaders()));
			borrowedBooks.setText(String.valueOf(pr[currentReportNumber-1].getNumberOfBorrowedBooks()));
			delayedInReturn.setText(String.valueOf(pr[currentReportNumber-1].getNumberOfDelayedInReturns()));
			dateLabel.setText(newDate.format(pr[currentReportNumber-1].getReportDate()));
			nextButton.setVisible(true);
			
				
		}//view the older report in DB
		else if (ae.getSource()==nextButton)
		{
			String pattern="dd/MM/yyyy";
			SimpleDateFormat newDate=new SimpleDateFormat(pattern);
			currentReportNumber++;
			if (currentReportNumber>=numOfReports)
			{
				nextButton.setVisible(false);
			}
			frozen.setText(String.valueOf(pr[currentReportNumber-1].getFrozenReaders()));
			locked.setText(String.valueOf(pr[currentReportNumber-1].getLockedReaders()));
			active.setText(String.valueOf(pr[currentReportNumber-1].getActivatedReaders()));
			borrowedBooks.setText(String.valueOf(pr[currentReportNumber-1].getNumberOfBorrowedBooks()));
			delayedInReturn.setText(String.valueOf(pr[currentReportNumber-1].getNumberOfDelayedInReturns()));
			dateLabel.setText(newDate.format(pr[currentReportNumber-1].getReportDate()));
			prevButton.setVisible(true);
		}
		
	}
	

}
